package com.course02.order_backend.security.LXY;



import com.course02.order_backend.entity.LXY.*;
import com.course02.order_backend.mapper.LXY.*;
import jakarta.annotation.Resource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wozhe
 * 从数据库中获取用户信息，将用户信息送到springSecurity上下文
 */
@Transactional
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        User user=userMapper.getUserByPhone(phone);
        if(Objects.isNull(user)){
            throw new UsernameNotFoundException(phone);
        }
        //当前用户角色名称列表、当前用户的权限名称列表
        List<String> roleCodeList=new ArrayList<>();
        List<String> permissionCodeList =new ArrayList<>();

        //获取用户角色列表，判空
        List<UserRole> userRoles = userRoleMapper.getUserRoleByUserId(user.getId());
        if(!CollectionUtils.isEmpty(userRoles)){
            List<Long> roleIdList = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toList());
            //如果不为空，那就批量
            if(!CollectionUtils.isEmpty(roleIdList)){
                //查询角色信息
                List<Role> roles = roleMapper.batchGetRolesByRoleId(roleIdList);
                //要角色名字，还是先判空,然后放入名字
                if(!CollectionUtils.isEmpty(roles)){
                    List<String> roleNames = roles.stream().map(Role::getRoleCode).collect(Collectors.toList());
                    roleCodeList.addAll(roleNames);
                }
                //查询当前角色下的权限列表
                List<RolePermission> rolePermissions = rolePermissionMapper.batchGetRolePermissionsByRoleIds(roleIdList);
                //如果不为空
                if(!CollectionUtils.isEmpty(rolePermissions)){
                    //拿到权限Id
                    List<Long> permissionIdList = rolePermissions.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
                    //判断是否不为空，拿到所有权限信息
                    if(!CollectionUtils.isEmpty(permissionIdList)){
                        List<Permission> permissions = permissionMapper.batchGetPermissionsByPermissionIds(permissionIdList);
                        //目的是权限的名字
                        if(!CollectionUtils.isEmpty(permissions)){
                            List<String> permissionCodes  = permissions.stream().map(Permission::getCode).collect(Collectors.toList());
                            if(!CollectionUtils.isEmpty(permissionCodes)){
                                permissionCodeList.addAll(permissionCodes);
                            }
                        }
                    }
                }
            }
        }

        return new LoginUserDetails(user,roleCodeList,permissionCodeList);
    }


}
